/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.gui.debugger;

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 *
 * @author blitzcoder
 */
public class DebugVariable extends DefaultMutableTreeNode {

    private String name;
    private String value;
    private String type;

    private LinkedList<DebugVariable> children;

    private DebugVariable parent = null;

    public DebugVariable(String name,String value) {
        
        int pos = name.indexOf(" ");

        if (pos != -1) {
            this.name = name.substring(pos);
            this.type = name.substring(0,pos);
        } else {
            this.type = "";
            this.name = name;
        }

        this.value = value;
        this.children = new LinkedList<DebugVariable>();
    }
    
    public void setParent(DebugVariable var) {
        parent = var;
    }

    public void removeChild(int num) {
        children.remove(children.get(num));
    }

    public void clearChildren() {
        children.clear();
    }
    
    public LinkedList<DebugVariable> getChildren() {
        return children;
    }
    
    public void addChild(DebugVariable child) {
        children.addLast(child);
        child.setParent(this);
    }

    /*
     * Output in the TreeView
     */
    @Override
    public String toString() {
        //if (value.length() != 0 && !value.startsWith("$")) // $xxxxxxx are memory-adresses, not interesting
            return "<html>"+type+" <b>"+name+"</b> = <b>"+value+"</b></html>";
        //return "<html>"+type+" <b>"+name+"</b></html>";
    }

    public String getName() {
        return name;
    }
    
    public String getValue() {
        return value;
    }

    @Override
    public TreeNode getChildAt(int childIndex) {
        return children.get(childIndex);
    }

    @Override
    public int getChildCount() {
        return children.size();
    }

    @Override
    public TreeNode getParent() {
        return parent;
    }

    @Override
    public void removeAllChildren() {
        children.clear();
    }

    @Override
    public int getIndex(TreeNode node) {
        ListIterator<DebugVariable> it = children.listIterator();
        int i = 0;
        while (it.hasNext()) {
            if (it.next() == node)
                return i;
            i++;
        }
        return 0;
    }

    @Override
    public boolean getAllowsChildren() {
        return true;
    }

    @Override
    public boolean isLeaf() {
        // if length() == 0, no value has been set. This must be the treeroot
        if (getValue().length() == 0)
            return false;

        // Else everything with an address is expandable
        return !getValue().startsWith("$");
    }

    @Override
    public Enumeration children() {
        return Collections.enumeration(children);
    }


}
